function jsonResponse(option = {}) {
    return async (ctx, next) => {
        ctx.success = function (data) {
            /*  */
            ctx.type = option.type || 'json'
            ctx.status = 200
            ctx.body = {
                code: 200,
                msg: option.successMsg || 'success',
                data: data
            }
        }
        ctx.fail = function (msg) {
            ctx.type = option.type || 'json'
            ctx.status = 400
            ctx.body = {
                code: 400,
                msg: msg || option.successMsg || 'fail',
                data: null
            }
        }
        await next()
    }
}

module.exports = jsonResponse
